:
eval 'exec perl -wS $0 ${1+"$@"}' if 0;

use POSIX qw(strftime);

# Subject to be changed by start parameters (to be done)
my $station_url = 'http://www.voanews.com/real/voa/eap/mand/mand1400a.ram';
#my $station_url = 'http://www.voanews.com/real/live/chinese.ram';
my $default_filename = 'chinese';
my $work_dir = '/tmp';
my $vsound_binary = '/usr/bin/vsound';
my $realplayer_binary = '/usr/bin/realplay';
my $mp3_enc_binary = '/usr/bin/lame';
my $mp3_enc_opts = '--vbr-new -q 0 -V 2 -m m';


my $date = strftime "%e.%b.%Y", localtime;
$date =~ s/\s//g;
my $file_name = ''; 
if (!$default_filename) {
    @url_entries = split(/\/+/, $station_url);
    my $station = $url_entries [0];
    $file_name = $url_entries [1] if ($station =~ /:/);
} else {
    $file_name = $default_filename;
};
$file_name .= '_' . $date;

$wav_filename = $work_dir . "/chinese.wav";
$mp3_filename = $work_dir . "/$file_name.mp3";

unlink $wav_filename;
do {
    system("$vsound_binary -f $wav_filename $realplayer_binary --new --quit $station_url");
    print;
} while (!-f $wav_filename);

system ("$mp3_enc_binary $mp3_enc_opts $wav_filename $mp3_filename");
unlink $wav_filename;
exit(0);



use File::Temp qw(tmpnam);

my $dir = shift @ARGV;
$dir =~ s/\/$//;
my $tmp_file = tmpnam();

open(TMPFILE, ">$tmp_file");
print TMPFILE $dir;
close TMPFILE;
open (DIRNAME, "/usr/bin/iconv -f windows-1251 -t koi8-r $tmp_file |");
my $new_dir_name = <DIRNAME>;
close DIRNAME;

opendir(DIRECTORY, $dir);
@dir_content = readdir(DIRECTORY);
my @new_dir_content;
closedir(DIRECTORY);
open(TMPFILE, ">$tmp_file");
foreach (@dir_content) {
    chomp;
    next if /^\.+$/;
    push(@new_dir_content, $_);
    print TMPFILE $_ . "\n";
};
close TMPFILE;

open (DIRCONTENT, "/usr/bin/iconv -f windows-1251 -t koi8-r $tmp_file |");
my $i = 0;
foreach (<DIRCONTENT>) {
#     print $_;
     chomp;
     my $old_path = $dir . '/' . $new_dir_content[$i];
     my $new_path = $dir . '/' . $_;
#     print "Rename $dir/". $new_dir_content[$i] . " to $dir/$_\n";
     rename($old_path, $new_path) or system("mv", $old_path, $new_path);
     $i++;     
};
close DIRCONTENT;

rename($dir, $new_dir_name) or system("mv", $dir, $new_dir_name);

unlink $tmp_file;
